package me.minicoral.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.sun.org.apache.bcel.internal.generic.Select;

import me.minicoral.action.Comment;
import me.minicoral.dao.CommentDao;
import me.minicoral.exception.DaoException;
import me.minicoral.util.DBCPUtil;

public class CommentDaoImpl implements CommentDao
{

	private QueryRunner qr = new QueryRunner(DBCPUtil.getDataSource());
	@Override
	public int addComment(Comment comment)
	{
		try
		{
			String sql = "insert into tcomment(cid,uuid,bid,ccomment,ccommenttime) values(?,?,?,?,SYSDATE)";
			Object param[] = {comment.getCid(),comment.getUuid(),comment.getBid(),comment.getCcomment()};
			return qr.update(sql, param);
		}
		catch (SQLException e)
		{
			throw new DaoException();
		}
	}
	
	@Override
	public List<Comment> findCommentByBid(String bid)
	{
		try
		{
			String sql = "select * from tcomment where bid = ?";
			Object param[] = {bid};
			return qr.query(sql, new BeanListHandler<Comment>(Comment.class),param);
		}
		catch (SQLException e)
		{
			throw new DaoException();
		}
	}

	public int getCommentCount(String bid)
	{
		try
		{
			String sql = "select count(*) from tcomment where bid=?"; 
			Object param = bid;
			System.out.println(qr.query(sql, new ScalarHandler(),param));
		}
		catch (SQLException e)
		{
			throw new DaoException();
		}
		return 0;
	}
}
